//
//  HttpRequest.m
//  极致荟
//
//  Created by hetao on 14-5-29.
//  Copyright (c) 2014年 mac. All rights reserved.
//

/*
 *
 *对SVHTTPRequest进行封装操作
 */
#import "HttpRequest.h"

@implementation HttpRequest

+ (void)httpRequestPost:(NSString *)URL parameters:(id)parameters block:(void (^)(id data,HttpReturnCode rerurncode))blok
{
    [SVHTTPRequest POST: URL parameters:parameters completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
        
        if(response!=nil)
        {   ///请求成功
            NSLog(@"%@",[[NSString alloc]initWithData:[NSData dataWithData: response ] encoding:NSUTF8StringEncoding] );
           
                NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:nil];
            if([[dic objectForKey:@"status"] intValue]==1)
            {
                blok(dic,HTTPReturnCodeSuccess);
            }else if([[dic objectForKey:@"status"] intValue]==-4)
            {
                blok(dic,HTTPReturnCodeUnknownError);
            }else if([[dic objectForKey:@"status"] intValue]==-3)
            {
                blok(dic,HTTPReturnCodeUploadFileError);

            }else
            {
                 blok(dic,HTTPReturnCodeCodeError);
            }
            
            
            
            
            
        }else
        {
            blok(nil,HTTPReturnCodeCodeError);
        }
        
    }];
}


/**上传新鲜事附件(图片语音)
 *
 *
 */
+(void)uploadTopicFile:(NSArray*)dataArray URL:(NSString *)jzhurl key:(NSString *)key block:(void (^)(id, HttpReturnCode))blok
{
    [MMProgressHUD showWithStatus:@"发布中..."];
    NSURL *url = [NSURL URLWithString:jzhurl];
    __block  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:[NSNumber numberWithInt: [User defuceUser].userId ] forKey:@"userId"];
    if (dataArray) {
        for (NSData *data in dataArray) {
            if ([self imageTypeFromData:data]) {
                NSString *Extension=[self imageTypeFromData:data];
                [request addData:data
                    withFileName:[@"file." stringByAppendingString:Extension]
                  andContentType:[@"image/" stringByAppendingString:Extension]
                          forKey:key];
            }else{
                [request addData:data withFileName:@"file.caf" andContentType:@"audio/caf" forKey:key];
            }
        }
    }
    [request setFailedBlock:^{
        
        [MMProgressHUD dismissWithError:@"发布失败"];
         NSLog(@"%@",request.responseString);
        
        }];
    
    [request setCompletionBlock:^{
        [MMProgressHUD dismissWithSuccess:@"发布成功"];
        NSLog(@"%@",request.responseString);
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingAllowFragments error:nil];
        if([[dic objectForKey:@"status"] intValue]==1)
        {
            blok(dic,HTTPReturnCodeSuccess);
        }else
        {
            blok(dic,HTTPReturnCodeCodeError);
        }

    }];
    [request startAsynchronous];
}

/**上传聊天附件(图片语音)
 *
 *
 */
+(void)uploadMessageFile:(NSData *)data URL:(NSString *)jzhurl key:(NSString *)key  block:(void (^)(id, HttpReturnCode))blok
{
    NSURL *url = [NSURL URLWithString:jzhurl];
    __block  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setStringEncoding:NSUTF8StringEncoding];
    if (data) {
            if ([self imageTypeFromData:data]) {
                NSString *Extension=[self imageTypeFromData:data];
                [request addData:data
                    withFileName:[@"file." stringByAppendingString:Extension]
                  andContentType:[@"image/" stringByAppendingString:Extension]
                          forKey:key];
                [request addPostValue:@"Image" forKey:@"fileType"];
            }else{
                //[request addFile:path forKey:key];
                [request addData:data withFileName:@"file.caf" andContentType:@"audio/caf" forKey:key];
                 [request addPostValue:@"Voice" forKey:@"fileType"];
            }
        
    }
    [request setFailedBlock:^{
        NSLog(@"%@",request.responseString);
        
    }];
    
    [request setCompletionBlock:^{
        NSLog(@"%@",request.responseString);
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingAllowFragments error:nil];
        if([[dic objectForKey:@"status"] intValue]==1)
        {
            blok(dic,HTTPReturnCodeSuccess);
        }else
        {
            blok(dic,HTTPReturnCodeCodeError);
        }
        
    }];
    [request startAsynchronous];
}

//判断图片格式
+(NSString*)imageTypeFromData:(NSData*)imageData{
    if (imageData.length > 4) {
        const unsigned char * bytes = [imageData bytes];
        
        if (bytes[0] == 0xff &&
            bytes[1] == 0xd8 &&
            bytes[2] == 0xff)
        {
            return @"jpg";//@"JPGE";
        }
        
        if (bytes[0] == 0x89 &&
            bytes[1] == 0x50 &&
            bytes[2] == 0x4e &&
            bytes[3] == 0x47)
        {
            return @"png";//@"PNG";
        }
    }
    
    return nil;
}
@end
